package com.example.mytestdemo.miv

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.mytestdemo.BaseDefaultFactory
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.*
import com.example.mytestdemo.linearlayoutmanager.dragon.CustomRecycleDragonActivity
import com.example.mytestdemo.softkey.SoftKeyActivity
import com.example.mytestdemo.viewpage2.ViewPager2ListAdapterActivity
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import java.util.*
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext


class TestMVIActivity : BaseViewBindActivity<ActivityTestMviLayoutBinding>() {


    companion object {
        private const val TAG = "TestMVIActivity"
    }

    val viewModel by lazy {
        ViewModelProvider(this).get(UiViewModel::class.java)
    }

    //    val viewModelMvi by lazy {
//        BaseDefaultFactory().create(UiMviViewModel::class.java)
//    }
    val viewModelMvi by lazy {
        ViewModelProvider(this).get(UiMviViewModel::class.java)
    }

    override fun getLayoutInflateId(): Int {
        return R.layout.activity_test_mvi_layout
    }

    override fun initData() {
        mBind.btRefresh.setOnClickListener {
            viewModelMvi.dispatch(MainIntent.refresh("参数哈哈哈"))
        }
        mBind.btLoad.setOnClickListener {
            viewModelMvi.dispatch(MainIntent.loadData)

//            runBlocking(Dispatchers.IO) {
//
//                mBind.btLoadFail.text = "子线程22${Thread.currentThread().name}"
//
//            }
        }

        mBind.btLoadFail.setOnClickListener {
            viewModelMvi.dispatch(MainIntent.loadFail)
        }
        mBind.btLoadFail.let {  }

        viewModelMvi.liveData.observe(this,{
            Toast.makeText(this@TestMVIActivity,"value 发送",Toast.LENGTH_SHORT).show()
        })


//        runBlocking(Dispatchers.IO) {
//
//            mBind.btLoadFail.text = "子线程22${Thread.currentThread().name}"
//
//        }
    }

    override fun onResume() {
        super.onResume()


    }
    override fun initUI() {
        Thread(){
            mBind.btLoadFail.text = "子线程可以更新viewRootImpl还未创建，没有进行主线程的检测"
        }.start()
        listenerModel()

        //mvi的方式
        listenerModelMvi()

    }


    private fun listenerModel() {
        lifecycleScope.launch {
            viewModel._isLoding.collect {
                Log.d(TAG, "listenerModel: 加载中 ")
            }
            viewModel._loadSuccess.collect {
                Log.d(TAG, "listenerModel: 加载成功 ")
            }

            viewModel._loadFailed.collect {
                Log.d(TAG, "listenerModel: 加载失败 ")
            }
        }
    }

    private fun listenerModelMvi() {
        lifecycleScope.launch {
            viewModelMvi.state.collect {
                when (it) {
                    MainUiState.isLoading -> {
                        Log.d(TAG, "listenerModelMvi: 加载中 ")
                        mBind.tvHint.text = "加载中"
                    }
                    is MainUiState.loadSuccess -> {
                        Log.d(TAG, "listenerModelMvi: 加载成功 ")
                        mBind.tvHint.text = "加载成功：${it.reqData.name}"

                    }
                    is MainUiState.loadError -> {
                        Log.d(TAG, "listenerModelMvi: 加载失败 ")
                        mBind.tvHint.text = "加载错误--${it.error}}"

                    }

                }
            }

        }
    }


}